﻿
#include <functional>
#include "tinyxml.h"
#include <winrt/base.h>
#include <filesystem>
#include <map>
#include <vector>
#include "Logging.h"
#include "VersionControl.h"


#pragma once
class Utility
{
public:
	
	static std::string tortoiseSVNPath; // TortoiseSVN路径
    static std::string comparePath; // Beyond Compare路径
    static std::string tortoiseGitPath; // TortoiseGit路径
    static std::string svnCliPath;  // svn.exe 路径（可为空=PATH）
    static std::string gitCliPath;  // git.exe 路径（可为空=PATH）
    static VcsType currentVcsType; // 当前版本控制类型
	static std::string lastSVNTarget; // 上次SVN目标路径
	static std::string lastSVNSuffix; // 上次SVN后缀
	static std::string lastMergeSource; // 上次合并源路径
	static std::string lastMergeTarget; // 上次合并目标路径
	static std::string lastMergeSuffix; // 上次合并后缀
	static std::string lastSearchExcelProj; // 上次选择路径
	static std::string svnAuthor; // SVN作者
	static std::string svnLogPath; // SVN日志路径


#ifdef DEBUG
	static constexpr const char ConfigXmlName[] = "Data\\Config.xml"; // xml配置文件名
#else
	static constexpr const char ConfigXmlName[] = "C:\\GMTK\\Config.xml"; // xml配置文件名
#endif // DEBUG


	static constexpr const char* data2Path = "gameData\\Data2"; // Data2路径
	static constexpr const char* clientPath = "\\Client"; // Client路径
	static constexpr const char* luaPath = "\\Client\\Assets\\Code_Lua"; // Lua路径
	static constexpr const char* csPath = "\\Client\\Assets\\Code"; // C#路径
	static constexpr const char* uiPrefabPath = "\\Client\\Assets\\Resources\\UI\\Prefabs"; // UI预const 制路径

    static constexpr const char* LAST_MERGE_SOURCE = "lastMergeSource"; 
    static constexpr const char* LAST_MERGE_TARGET = "lastMergeTarget";
    static constexpr const char* LAST_MERGE_SUFFIX = "lastMergeSuffix";
    static constexpr const char* LAST_SVN_TARGET = "lastSVNTarget";
    static constexpr const char* LAST_SVN_SUFFIX = "lastSVNSuffix";
	static constexpr const char* OFFEN_USE_EXCEL = "OffenUse";
	static constexpr const char* LAST_SEARCH_EXCEL_PROJ = "lastSearchExcelProj";
    // logging config keys
    static constexpr const char* LOG_MIN_LEVEL = "logMinLevel"; // values: Debug/Info/Warn/Error or 0/1/2/3
    static constexpr const char* LOG_CONSOLE_ENABLED = "logConsoleEnabled"; // 0/1
    static constexpr const char* LOG_FILE_ENABLED = "logFileEnabled"; // 0/1
    static constexpr const char* LOG_FILE_PATH = "logFilePath"; // string path
	

public:
	static void Init();

	static std::string GetConfigXmlPath();

	/// <summary>
	/// 运行TortoiseSVN命令
	/// </summary>
	/// <param name="command">命令</param>
	/// <param name="path">路径</param>
    static void RunTortoiseSVNCommand(const std::string& command, const std::string& path);
    static void RunTortoiseSVNMerge(const std::string& path, const std::string& target);

    // 统一 VCS 操作封装
    static void RunVcsUpdate(const std::string& path);
    static void RunVcsCommit(const std::string& path);
    static void RunVcsLog(const std::string& path);
    static void RunVcsRevert(const std::string& path);
    static void RunVcsCleanup(const std::string& path);
    static void RunVcsMerge(const std::string& source, const std::string& target);

	static void CopySvnLog(const std::string& author, std::function<void(winrt::hstring)> callback);

	static std::filesystem::path GetSearchRoot();

    // 日志相关请直接使用 Logging 模块

	static bool IsProjNameExist(const std::string& name);
	static bool IsSuffixExist(const std::string& suffix);

	static inline const std::string& GetWorkingDirectory()
	{
		return WorkingDirectory;
	}

	static inline std::map<std::string, std::string>& GetProjPathMap()
	{
		return projPathMap;
	}

	static inline std::map<std::string, std::string>& GetUrlMap()
	{
		return urlMap;
	}

	static inline std::map<std::string, std::string>& GetSuffixMap()
	{
		return suffixMap;
	}

	static inline std::vector<winrt::hstring>& GetOffenUseExcelList()
	{
		return offenUseExcelList;
	}

	static void SetLastSVNTarget(const std::string& target);
	static void SetLastSVNSuffix(const std::string& suffix);

	static void SetLastMergeSource(const std::string& source);
	static void SetLastMergeTarget(const std::string& target);
	static void SetLastMergeSuffix(const std::string& suffix);

	static void SetLastSearchExcelProj(const std::string& proj);

	static void SetTortoiseSVNPath(const std::string& path);
    static void SetComparePath(const std::string& path);
    static void SetTortoiseGitPath(const std::string& path);
    static void SetVcsType(VcsType type);
    static inline VcsType GetVcsType() { return currentVcsType; }
    static void SetSvnCliPath(const std::string& path);
    static void SetGitCliPath(const std::string& path);

	static void SetSvnAuthor(const std::string& author);

    // Logging settings (persist + apply)
    static void SetLogMinLevel(Logging::Level level);
    static void SetLogConsoleEnabled(bool enabled);
    static void SetLogFileEnabled(bool enabled);
    static void SetLogFilePath(const std::string& path);

    // 确保缺省日志配置（仅在缺失时写入，不覆盖已有配置）
    static void EnsureDefaultLoggingConfig();

	static std::vector<winrt::hstring> SearchExcel(std::filesystem::path& searchRoot, const std::wstring& keyword);

	static void OpenFolder(const std::string& path);
	static void OpenXlsmWithExcel(const std::filesystem::path path);
	static void AddOffenUse(const winrt::hstring& name);
	static void AddProj(const std::string name, const std::string path, const std::string url);
	static void AddSuffix(const std::string suffix, const std::string path);
	static void RemoveProj(const std::string name);
	static void RemoveSuffix(const std::string suffix);
	static void RemoveOffenUse(const winrt::hstring& name);
	static bool IsOffenUse(const winrt::hstring& name);
	static void StartProcess(std::wstring arguments);
    static void StartProcess(const std::string& arguments);
    static void StartProcessAsync(const std::string& arguments);
    static void StartProcessAsync(const std::wstring& arguments);

    // 为 UI 层提供错误提示回调注册入口（由页面在 OnLoad 中注册）
    static void SetUiErrorHandler(std::function<void(const winrt::hstring&)> handler);

private:
    static std::string WorkingDirectory;

	static std::map<std::string, std::string> projPathMap; // 项目路径映射
	static std::map<std::string, std::string> urlMap; // URL映射
	static std::map<std::string, std::string> suffixMap; // 后缀映射
	static std::vector<winrt::hstring> offenUseExcelList; // 常用Excel列表
private:
    static void InitXmlCfg();
	static std::unordered_map<std::string, std::function<void(TiXmlElement*)>> GetElementHandlers();
	static void SetXmlCfg(std::string elementName, std::string elementValue);

    // UI 错误提示回调
    static inline std::function<void(const winrt::hstring&)> s_uiErrorHandler{};
    static inline void NotifyUiError(const std::wstring& message)
    {
        if (s_uiErrorHandler)
        {
            s_uiErrorHandler(winrt::hstring(message));
        }
    }
};

// DEBUG_LOG 宏已移除，请直接使用 Logging 模块的 LOG_DEBUG/LOG_INFO/LOG_WARN/LOG_ERROR
